<?php
@session_start();
//require_once('../user/wxbrower.php');
require_once("../includes/public.inc.php");
$s = isset($_GET['s'])?$_GET['s']:'';
$username = isset($_SESSION['username'])?$_SESSION['username']:'';
$sysconfig=mysql_fetch_array(mysql_query("select * from ".DBQIAN."sys_config limit 1"));
if (!$s)
{
	echo "该二维码为非法防伪码，购买的产品请谨慎使用!";exit;
	//echo "<script type='text/javascript'>alert('该二维码为非法防伪码，请谨慎使用!');return false;</script>";exit;
}
//if (!isset($_GET['code']))
//{
//	$uwxcode=$_GET['uwxcode'];
//	$locaurl=urlencode(WEBNAME."main/mns.php?s=".$s."&uwxcode=".$uwxcode);//网址解析
//	$urls="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".trim($sysconfig['cappid'])."&redirect_uri=".$locaurl."&response_type=code&scope=snsapi_base#wechat_redirect";
//	header("Location:".$urls);exit;
//}
//$code = $_GET['code'];//获取code
//$weixin =  file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$sysconfig['cappid']."&secret=".$sysconfig['cappsecret']."&code=".$code."&grant_type=authorization_code");//通过code换取网页授权access_token
//$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
//$array = get_object_vars($jsondecode);//转换成数组
//$openid = $array['openid'];//输出openid

	
function getAccessToken()
{
	$sysconfig=mysql_fetch_array(mysql_query("select * from ".DBQIAN."sys_config limit 1"));
	 if ($sysconfig['ctokentime'] < time()) {
      // 如果是企业号用以下URL获取access_token
      // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$sysconfig['cappid']."&secret=".$sysconfig['cappsecret'];
      $res = json_decode(curlGet($url));
      $access_token = $res->access_token;
      if ($access_token) {
        $ctokentime = time() + 7000;
        $ctoken = $access_token;
		mysql_query("update ".DBQIAN."sys_config set ctoken='$ctoken',
					  ctokentime='$ctokentime', where id=1");
        //$fp = fopen("access_token.json", "w");
       // fwrite($fp, json_encode($data));
        //fclose($fp);
      }
    } else {
      $access_token = $sysconfig['ctoken'];
    }
    return $access_token;
}

function curlGet($url)
{
	$curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
}

//$access_token=getAccessToken();
//$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid;
//$subscribe = json_decode(curlGet($subscribe_msg),true);
//$zyxx = $subscribe['subscribe'];
//if($zyxx !== 1)
//{
//	header("Location: ../user/guanzhu.php");exit;
//}

//判断是否关注

//echo $openid;exit;
//开始是查询防伪码的
/*使用防伪码进行查询，如果数据库有这个码，然后放到随机库里面查询，如果有就中奖没有就写很遗憾没有中奖randomcode*/
$exsit_openid = mysql_fetch_array(mysql_query("select * from ".DBQIAN."users where openid='".$openid."' order by id desc limit 1"));
$fwm = mysql_fetch_array(mysql_query("select id,fwm,time from ".DBQIAN."fwm where fwm='".trim($s)."' and userstatus=0 order by id desc limit 1"));//未使用,未领取奖品的
$fwm1 = mysql_fetch_array(mysql_query("select id,fwm,time from ".DBQIAN."fwm where fwm='".trim($s)."' and userstatus=1 order by id desc limit 1"));//已使用
if (!$fwm && !$fwm1)//无效防伪码，不存在
{
	header("Location: mu_tip.php?status=2&s=".$s);exit;
}
else 
{
	if ($fwm)//未使用，未领取奖品的
	{
		//暂时不需要弹窗提示
//		echo "<script>alert('该二维码为正品，请放心使用!');</script>";
		if (!empty($username))
		{
			header("Location: zhengpu.php?s=".trim($s)."&openid=".$openid);exit;
		}
		else
		{
			if ($exsit_openid['id'])
			{
				$_SESSION['username'] = $exsit_openid['username'];
				header("Location: zhengpu.php?s=".trim($s)."&openid=".$openid);exit;
			}
		}
	}
	else 
	{
		if ($fwm1)//已使用
		{
			header("Location: mu_tip.php?status=1&s=".$s);exit;
		}
	}
}

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="user-scalable=no,width=device-width, initial-scale=1.0 maximum-scale=1.0">
<meta name="Keywords" content="正品验证" />
<meta name="Description" content="正品验证" />
<title>卡玫尔</title>
<link rel="stylesheet" href="Public/css/home.min.css" />
<script src="./Public/js/jquery-1.7.2.min.js"></script>
<script language="javascript" type="text/javascript" src="Public/js/sel.js"   charset="gb2312"></script>
<script type="text/javascript">
	var ua = navigator.userAgent;
	var ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
    isIphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
    isAndroid = ua.match(/(Android)\s+([\d.]+)/),
    isMobile = isIphone || isAndroid;
    if(isMobile) 
    {
        return true;
    }
    else
    {
        location.href = 'tip.php';
    }
    //或者单独判断iphone或android 
//    if(isIphone){
//        //code 
//    else if(isAndroid){
//        //code
//    }else{
//        //code
//    }
</script>
<style type="text/css">
.td{border:solid #000; border-width:0px 1px 1px 0px; padding:10px 0px;}
.table{border:solid #000; border-width:1px 0px 0px 1px;}
input
{
	BACKGROUND-COLOR: transparent;
	BORDER-BOTTOM: #000 1px solid;
	BORDER-LEFT: #000 1px solid;
	BORDER-RIGHT: #000 1px solid;
	BORDER-TOP: #000 1px solid;
	COLOR: #FFF;
	WIDTH:200px;
	HEIGHT: 28px;
	border-color: #FFD300 #FFD300 #FFD300 #FFD300; font-size: 9pt;
	-moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius:5px;
}
#logo
{
	max-width:100%;
	max-height:100%;
	max-width: 100%;
	height: auto;
	width: auto\9;
}
</style>
</head>
<body class="ui-bar-a" style="background:orange;">
<img src="../images/logo.png" style="margin-left:17px; margin-top:9px;" id="logo" width="100px" height="55px"/>
<div class="load_box" style="margin-top:-245px; width:280px; text-align:center;margin-left:-140px;">
	<div style="margin-left:auto;margin-right:auto;top:7px;text-align:center;font-size:19px;font-weight:bolder;">
			
	</div>
</div>

<div style="width:86%;  margin-left:auto;margin-right:auto;">
	<div style="width:100%;text-align:center;margin-top:13px;">
		<table width="100%" style="margin-left:auto;margin-right:auto;background-color:#000;opacity: 0.58;-moz-border-radius: 10px;-webkit-border-radius: 10px;border-radius:10px;">
			<tr >
				<td style="color:#fff;">查询结果:</td><td width="228px"  style="color:#FD7297;font-size:12px;font-weight:bolder;"  align="left">您好!你所查询的产品为正品,请放心使用！</td>
			</tr>
			<tr >
				<td width="72px" style="color:#fff;">防&nbsp;&nbsp;伪&nbsp;&nbsp;码:</td><td width="228px" align="left"><span style="color:#FD3F72;font-size:15px; font-weight:bolder;"><?=$s?></span></td>
			</tr>
			<tr >
				
			</tr>
			<tr>
				<td style="color:#fff;">产品品牌:</td><td align="left"><span style="width:100%;color:#FFD300; font-size:13px; font-weight:bolder;margin-left:auto;margin-right:auto;text-align:center;">卡玫尔</span></td>
			</tr>
		</table>
	</div>
</div>
<div style="width:86%; margin-left:auto;margin-right:auto;">
	<div style="width:100%; text-align:center;margin-top:2px;">
		<table width="100%" style="margin-left:auto;margin-right:auto;background-color:#000;opacity: 0.78;-moz-border-radius: 6px;-webkit-border-radius: 6px;border-radius:6px;">
			<tr>
				<td style="color:#FD7297; font-weight:bolder;" colspan="2"  height="40px">
				</td>
			</tr>
			
			<!--<tr>
				<td style="color:#FD7297; font-weight:bolder;" colspan="2" align="center"  height="40px">
					<input type="button" onclick="window.location.href='login.php?s=<?=$s?>&openid=<?=$openid?>'" style="font-size:14px;font-weight:bolder; background-color:#FC0044;border-color:#BD2F55; width:80%;height:32px;" value="会员登录" >
				</td>
			</tr>-->
			<?if (!$exsit_openid):?>
			<tr>
				<td style="color:#FD7297; font-weight:bolder;" colspan="2" align="center"  height="40px">
					<input type="button" onclick="window.location.href='userget.php?s=<?=$s?>&openid=<?=$openid?>'" style="font-size:14px;font-weight:bolder; background-color:#FC0044;border-color:#BD2F55; width:80%;height:32px;" value="会员注册" >
				</td>
			</tr>
			<?endif;?>
			<tr>
				<td style="color:#FFD300; font-size:10px;" colspan="2" align="center"  height="40px">该活动的最终解释权归卡玫尔所有</td>
			</tr>
		</table>
	</div>
<div class="load_box" style="margin-top:15px;height:120px; position:relative;">
	<!--<div class="liwuso_logo"><img src="../images/t01739705f991e0d14f.gif" width="90%" /></div>-->
</div>
</div>

<footer id="footer" class="index_footer" style="position:relative;">
	<div class="copy">
		<div style="margin-left:-90px">
			<img src="../images/mom.jpg"  width="100" height="100" /><span style="float:left;position:absolute;width:100px;font-size:14px;height:50px;text-align:left;margin-top:0px;">#长按关注#或者打开微信客户端搜索微信公众号<span style="color:red;">卡玫尔</span>进行关注了解更多</span><br>
		</div>
		<span style="color:#000">&copy;2016 卡玫尔 All Rights Reserved.</span>
	</div>
</footer>
</body>
</html>